package com.unknow.first.dapp.admin.account.controller.admin;

import static com.unknow.first.dapp.admin.account.constants.AddressAccountMenuCode.PARENT_CODE;
import static com.unknow.first.dapp.admin.account.constants.AddressAccountMenuCode.SYSTEM_ADDRESS_CODE;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.lianziyou.chain.common.api.CommonPage;
import com.lianziyou.chain.common.api.CommonParam;
import com.unknow.first.dapp.admin.account.constants.ISystemAddressTypeEnum;
import com.unknow.first.dapp.admin.account.dto.SystemAddressQueryDTO;
import com.unknow.first.dapp.admin.account.mapper.TSystemAddress;
import com.unknow.first.dapp.admin.account.service.SystemAddressTypeService;
import com.unknow.first.dapp.admin.account.service.TSystemAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.cloud.annotation.SystemResource;
import org.cloud.constant.CoreConstant;
import org.cloud.constant.CoreConstant.AuthMethod;
import org.cloud.context.RequestContextManager;
import org.cloud.utils.MyBatisPlusUtil;
import org.cloud.vo.CommonApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin/system/address")
@Slf4j
@SystemResource(path = "/admin/system/address", parentMenuName = "资金管理", parentMenuCode = PARENT_CODE)
@Api(tags = "管理员：系统账户管理", value = "AdminSystemAddressController")
public class AdminSystemAddressController {

    @Autowired
    TSystemAddressService systemAddressService;

    @SystemResource(value = "/list", description = "管理员查询系统用户列表", authMethod = CoreConstant.AuthMethod.BYUSERPERMISSION, menuName = "系统账户管理", menuCode = SYSTEM_ADDRESS_CODE)
    @GetMapping
    @ApiOperation("查询系统用户列表")
    public CommonApiResult<CommonPage<TSystemAddress>> list(SystemAddressQueryDTO systemAddressQueryDTO, CommonParam pageParam) {
        QueryWrapper<TSystemAddress> queryWrapper = MyBatisPlusUtil.single().getPredicate(systemAddressQueryDTO);
        PageHelper.startPage(pageParam.getPage(), pageParam.getLimit(), pageParam.getSorts());
        return CommonApiResult.createSuccessResult(CommonPage.restPage(systemAddressService.list(queryWrapper)));
    }

    @SystemResource(value = "/create", description = "创建系统用户", authMethod = CoreConstant.AuthMethod.BYUSERPERMISSION)
    @PostMapping
    @ApiOperation("创建系统用户")
    public CommonApiResult<Boolean> create(@RequestBody TSystemAddress systemAddress) {
        systemAddress.setCreatedBy(RequestContextManager.single().getRequestContext().getUser().getUsername());
        systemAddress.setUpdatedBy(RequestContextManager.single().getRequestContext().getUser().getUsername());
        systemAddress.setId(null);
        return CommonApiResult.createSuccessResult(systemAddressService.save(systemAddress));
    }

    @SystemResource(value = "/update", description = "更新系统用户", authMethod = CoreConstant.AuthMethod.BYUSERPERMISSION)
    @PutMapping
    @ApiOperation("更新系统用户")
    public CommonApiResult<Boolean> update(@RequestBody TSystemAddress systemAddress) {
        Assert.notNull(systemAddress.getId(), "id不能为空");
        systemAddress.setUpdatedBy(RequestContextManager.single().getRequestContext().getUser().getUsername());
        systemAddress.setAddress(null); // 不能更新系统地址，只能禁用后重新增加，防止带来不必要的错误
        return CommonApiResult.createSuccessResult(systemAddressService.updateById(systemAddress));
    }

    @Autowired
    SystemAddressTypeService systemAddressTypeService;

    @GetMapping("/listSystemAddressType")
    @SystemResource(value = "/listSystemAddressType", description = "查询系统账户类型列表", authMethod = AuthMethod.ALLSYSTEMUSER)
    @ApiOperation("查询系统账户类型列表")
    public CommonApiResult<List<ISystemAddressTypeEnum>> listSystemAddressType() throws Exception {
        return CommonApiResult.createSuccessResult(systemAddressTypeService.listSystemAddressType());
    }

}
